package com.wang.dynamic.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * SqlSessionFactory配置
 *  1、指定mapper文件所在位置
 *  2、指定使用的SqlSessionFactory
 *
 * @author: wangning1@abupdate.com
 * @date: 2023-12-19 17:41
 **/
@Configuration
@MapperScan(basePackages = "com.wang.dynamic.mapper.source1",
        sqlSessionFactoryRef = "sqlSessionFactory1")
public class Source1Config {

    @Bean(name = "dataSource1")
    @ConfigurationProperties(prefix = "spring.datasource.ds1")
    public DataSource dataSource1(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public SqlSessionFactory sqlSessionFactory1() throws Exception{
        final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource1());
        //指定xml配置文件位置
        // sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
        //         .getResources("classpath:mapper/source1/*.xml"));

        return sqlSessionFactoryBean.getObject();
    }

}